const appConfig = require('../../config/config.js');
const http = require('../../utils/http.js');
const nav = require('../../utils/navigator.js');
const util = require('../../utils/util.js');
// 引入SDK核心类
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentUserId:0,//圈主ID
    txtUserName: '',
    txtUserImg: '',
    hiddenModal: true,
    pageIndex: {}, //当前页码
    pageData: {}, //分页数据
    loadMoreText: '正在载入更多...', //上拉加载提示文本
    hideBottom: true, //上拉加载提示层
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.userId);
    var that = this;
    that.setData({ currentUserId: options.userId});
    http.post(appConfig.baseUrl + 'Login/GetUserInfoById', {
      id: that.data.currentUserId
    }, function (res) {
      if (res) {
        console.log(res.data);
        that.setData({
          txtUserName: res.data.NickName,
          txtUserImg: res.data.AvatarUrl
        });
      }
    }, function (res) {

    });
    //
    wx.showLoading({
      title: '玩命加载中...',
    });
    //列表加载
    http.post(appConfig.baseUrl + 'Article/QueryArticleList', {
      PageIndex: 1,
      userId: that.data.currentUserId
    },
      function (res) { //
        console.log(res.data);
        wx.hideLoading(); //关闭加载动画
        that.setData({
          pageIndex: 1, //当前页码
          pageData: res.data, //分页数据
          hideBottom: true,
          loadMoreText: '正在载入更多...'
        });

      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();

    http.post(appConfig.baseUrl + 'Article/QueryArticleList', {
      PageIndex: 1,
      userId: that.data.currentUserId
    },
      function (res) { //
        console.log(res.data);
        that.setData({
          pageIndex: 1, //当前页码
          pageData: res.data, //分页数据
          hideBottom: true,
          loadMoreText: '正在载入更多...'
        });
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 页数+1
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      hideBottom: false
    });

    console.log(that.data.pageIndex);
    http.post(appConfig.baseUrl + 'Article/QueryArticleList', {
      PageIndex: that.data.pageIndex,
      userId: that.data.currentUserId
    },
      function (res) { //
        console.log(res.data);
        if (res.data.length > 0) {
          var data_list = that.data.pageData;
          for (var i = 0; i < res.data.length; i++) {
            data_list.push(res.data[i]);
          }
          that.setData({
            pageData: data_list, //分页数据
            hideBottom: true
          });
          console.log(that.data.pageData);
        } else {
          //没有更多了
          that.setData({
            loadMoreText: "没有更多数据了", //分页数据
            hideBottom: false,
            pageIndex: that.data.pageIndex - 1
          });
        }
      });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})